package modules

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

var ginApps = map[string]GinService{}

type GinService interface {
	GinRegistry(r gin.IRouter)
	GinName() string
	GinConfig()
}

func RegistryGins(svc GinService) {
	if _, ok := ginApps[svc.GinName()]; ok {
		panic(fmt.Sprintf("service %s has registried", svc.GinName()))
	}
	ginApps[svc.GinName()] = svc
}

func InitGins(r gin.IRouter) {
	for _, v := range ginApps {
		// 先初始化好所有对象
		v.GinConfig()
		// 再完成Http Handler的注册
		v.GinRegistry(r)
	}
}

func LoadGinApps() (names []string) {
	for k := range ginApps {
		names = append(names, k)
	}

	return names
}
